<template>
  <el-tabs v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="角色管理" name="first">
      <div class="dashboard-container">
        <div class="app-container">
          <el-card>
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button type="primary" size="small" @click="addRole">新增角色</el-button>
            </el-row>
            <el-table
              :data="list"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                label="序号"
                width="240"
                align="center"
              />
              <el-table-column
                prop="name"
                label="角色名称"
                width="250"
                align="center"
              />
              <el-table-column
                prop="description"
                label="角色描述"
                width="360"
                align="center"
              />
              <el-table-column label="操作" width="500" fixed="right">
                <template #default="scoped">
                  <el-button size="small" type="success" @click="showAssignPermission(scoped.row)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(scoped.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delFn(scoped.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-row type="flex" justify="center" align="middle" style="height:60px">
              <el-pagination
                :page-size="pageOption.pagesize"
                :total="total"
                :current-page="pageOption.page"
                layout="prev, pager, next"
                hide-on-single-page
                @current-change="currentChangeFn"
              />
            </el-row>
            <!-- 新增角色的对话框 -->
            <el-dialog
              :title="title"
              :visible="showDialog"
              width="40%"
              @close="closeDialog"
            >
              <el-form ref="formRef" :model="formData" :rules="formRules">
                <el-form-item label="角色名称" prop="name" label-width="120px">
                  <el-input v-model="formData.name" />
                </el-form-item>
                <el-form-item label="角色描述" prop="description" label-width="120px">
                  <el-input v-model="formData.description" />
                </el-form-item>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
              </span>
            </el-dialog>
            <AssignPermission :role-id="roleId" :visible="assignPermissionVisible" @dialog-close="assignPermissionClose" />
          </el-card>
        </div>
      </div>
    </el-tab-pane>
    <el-tab-pane label="公司信息" name="second">公司信息</el-tab-pane>

  </el-tabs>

</template>

<script>
import AssignPermission from './component/assign-permission'
import { getRoleList, addRoles, updateRoles, getDetail, delRoles } from '@/api'
export default {
  name: 'Dashboard',
  components: { AssignPermission },
  data() {
    return {
      roleId: '',
      activeName: 'first',
      list: [],
      pageOption: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      showDialog: false, // 控制对话框的显示隐藏
      formData: {
        name: '',
        description: ''
      },
      // 校验规则
      formRules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      // currentNode: {}
      // 控制分配权限弹窗显示与隐藏
      assignPermissionVisible: false
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.fetchRoleList()
  },
  methods: {
    // 打开分配权限弹窗
    showAssignPermission(row) {
      this.assignPermissionVisible = true
      this.roleId = row.id
    },
    // 分配权限弹窗关闭
    assignPermissionClose() {
      this.assignPermissionVisible = false
    },
    // 根据id编辑角色
    async edit(val) {
      // console.log(val)
      // this.currentNode = val
      const { data } = await getDetail(val.id)
      // console.log(data)
      this.formData = data
      this.showDialog = true
    },
    // 分页
    currentChangeFn(val) {
      this.pageOption.page = val
      this.fetchRoleList()
    },
    // 获取角色列表数据
    async fetchRoleList() {
      const { data } = await getRoleList(this.pageOption)
      // console.log(data)
      this.total = data.total
      this.list = data.rows
    },
    handleClick() {

    },
    // 点击添加角色
    addRole() {
      this.showDialog = true
    },
    // 关闭dialog
    closeDialog() {
      this.showDialog = false
      this.$refs.formRef.resetFields()
      this.formData = {
        name: '',
        description: ''
      }
    },
    // 点击确定提交
    submit() {
      this.$refs.formRef.validate(async(valid) => {
        if (!valid) return
        console.log(this.formData)
        if (this.formData.id) {
          // 编辑角色
          await updateRoles(this.formData)
        } else {
          // 新增角色
          await addRoles(this.formData)
        }
        this.$message.success(this.title + '成功！')
        this.fetchRoleList()
        this.closeDialog()
      })
    },
    // 点击删除
    delFn(val) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoles(val.id)
        this.fetchRoleList()
        this.$message.success('删除角色成功')
        if (this.list.length === 1 && this.pageOption.page !== 1) {
          this.pageOption.page--
        }
      }).catch(() => {
        this.$message.error('取消删除')
      })
    }
  }
}
</script>
